<template>
  <div class="app-container">
    <el-form :inline="true" :model="brandListRequest" class="demo-form-inline">
      <el-form-item label="品牌名称/关键词">
        <el-input v-model="brandListRequest.brandName" placeholder="品牌名称/关键词"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productBrandList" @selection-change="handleSelectionChange">
      <el-table-column label="品牌编号" align="center" prop="productBrandId"/>
      <el-table-column label="品牌名称" align="center" prop="brandName"/>
      <el-table-column label="品牌首字母" align="center" prop="brandInitial"/>
      <el-table-column label="排序" align="center" prop="isSort"/>
      <el-table-column label="品牌制造商" align="center" prop="brandManufacturer">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.brandManufacturer"
            :active-value="0"
            :inactive-value="1"
            @change="updateBrandManufacturer(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="显示" align="center" prop="isShow">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShow"
            :active-value="0"
            :inactive-value="1"
            @change="updateBrandManufacturer(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:post:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:post:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[3, 5, 7, 10]"
      @size-change="handleSizeChange"
      :page.sync="brandListRequest.pageNum"
      :limit.sync="brandListRequest.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="productBrand" :rules="rules" label-width="80px">
        <el-form-item label="品牌名称" prop="brandName">
          <el-input v-model="productBrand.brandName" placeholder="请输入品牌名称"/>
        </el-form-item>
        <el-form-item label="品牌首字母" prop="brandInitial">
            <el-input v-model="productBrand.brandInitial" placeholder="请输入品牌首字母"/>
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="brandLogo">
          <imageUpload v-model="productBrand.brandLogo" :limit="1"></imageUpload>
        </el-form-item>
        <el-form-item label="品牌专区大图" prop="brandImage">
          <imageUpload v-model="productBrand.brandImage" :limit="1"></imageUpload>
        </el-form-item>
        <el-form-item label="品牌故事" prop="brandStory">
          <el-input v-model="productBrand.brandStory" placeholder="请输入品牌故事"/>
        </el-form-item>
        <el-form-item label="排序" prop="isSort">
          <el-input v-model="productBrand.isSort" placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-radio v-model="productBrand.isShow" :label="0">是</el-radio>
          <el-radio v-model="productBrand.isShow" :label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="品牌制造商" prop="brandManufacturer">
          <el-radio v-model="productBrand.brandManufacturer" :label="0">是</el-radio>
          <el-radio v-model="productBrand.brandManufacturer" :label="1">否</el-radio>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import ImageUpload from "../../../components/ImageUpload/index.vue";
import {
  addProductBrand, deleteProductBrand,
  findProductBrandList,
  updateProductBrand
} from "../../../api/product/productBrand/productBrand";
import {deleteProductType} from "../../../api/product/productType/productType";

export default {
  name: "ProductBrand",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
      //上传图片
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品分类表格数据
      productBrandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      brandListRequest: {
        brandName: '',
        pageNum: 1,
        pageSize: 5
      },
      // 表单参数
      productBrand: {},
      // 表单校验
      rules: {
        postName: [
          {required: true, message: "岗位名称不能为空", trigger: "blur"}
        ],
        postCode: [
          {required: true, message: "岗位编码不能为空", trigger: "blur"}
        ],
        postSort: [
          {required: true, message: "岗位顺序不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  comments:{
   imageUpload:ImageUpload
  },
  methods: {
    updateBrandManufacturer(row) {
      console.log(row);
      if (row.productBrandId != undefined) {
        updateProductBrand(row).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        });
      }
    }, handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.brandListRequest.pageSize = val;
      console.log(this.brandListRequest);
      this.getList()
    },
    /** 查询商品品牌列表 */
    getList() {
      this.loading = true;
      findProductBrandList(this.brandListRequest).then(response => {
        this.productBrandList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    }, /** 查询下级商品分类列表 */
    getNextList(row) {
      this.typeListRequest.parentId = row.productTypeId
      this.loading = true;
      getProductTypeList(this.typeListRequest).then(response => {
        if(response.data.list.length<=0){
          this.$message.warning("该商品没有下级")
        }else {
          this.productTypeList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // this.firstList = []
      this.productBrand = {
        productBrandId: 0,
        brandName: '',
        brandInitial: '',
        brandLogo: '',
        brandImage: '',
        brandStory: '',
        isSort: undefined,
        isShow: undefined,
        brandManufacturer: undefined,
        isDelete: 0
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.productBrand = row;
      console.log(this.productBrand);
      this.open = true;
      this.title = "修改商品品牌";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      this.open = true;
      this.title = "添加商品品牌";
    },
    /** 提交按钮 */
    submitForm: function () {
      console.log(this.productBrand)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.productBrand.productBrandId != 0) {
            updateProductBrand(this.productBrand).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductBrand(this.productBrand).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除岗位编号为"' + row.productBrandId + '"的数据项？').then(function () {
        return deleteProductBrand(row.productBrandId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
